/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AssignComputerManual.java
 *
 * Created on 20/06/2011, 09:47:35 AM
 */

package GUI.ATTENDANT;

import GUI.Customer.Server;
import Procedures.DBConection;
import Procedures.LoanTable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author SIX-14
 */
public class AssignComputerManual extends javax.swing.JPanel {

    /** Creates new form AssignComputerManual */
    public AssignComputerManual() {
        initComponents();
    }
    public AssignComputerManual(DBConection myConection, JFrame paternFrame, LoanTable loantable, Server server) {
        initComponents();
        this.myConection=myConection;
        this.paternFrame=paternFrame;
        this.loanTable=loantable;
        this.server=server;
        fillcomboboxes();
    }


    private DBConection myConection;
    private JFrame paternFrame;
    private LoanTable loanTable;
    private Server server;

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public LoanTable getLoanTable() {
        return loanTable;
    }

    public void setLoanTable(LoanTable loanTable) {
        this.loanTable = loanTable;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }


    public void fillcomboboxes(){
        getMyConection().sendSQLQuery("SELECT name FROM TYPE_USER;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox1.removeAllItems();
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
        getMyConection().sendSQLQuery("SELECT NUMBER FROM COMPUTER WHERE STATE=1;");
        elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox2.removeAllItems();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Introduzca clave de usuario");

        jLabel2.setText("Elija tipo de usuario");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Elija computadora a prestar:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Realizar  Prestamo");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(45, 45, 45)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2)))
                            .addComponent(jLabel3)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jButton1)))
                .addContainerGap(119, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(143, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(!getLoanTable().find_user(Integer.parseInt(jTextField1.getText()), jComboBox1.getSelectedItem().toString())){
            getMyConection().sendSQLQuery("SELECT ACTIVE FROM STUDENT WHERE ID="+jTextField1.getText()+";");
            String[] valor=getMyConection().getLastResultSetColumnStrings(1);
            if(valor!=null){
                if(Boolean.parseBoolean(valor[0])){
                    getMyConection().sendSQLQuery("SELECT IP FROM COMPUTER WHERE NUMBER="+jComboBox2.getSelectedItem().toString()+";");
                    String ip=getMyConection().getLastResultSetColumnStrings(1)[0];
                    getMyConection().sendSQLQuery("SELECT ID FROM APLICATION;");
                    String[] process=getMyConection().getLastResultSetColumnStrings(1);
                    getServer().getComstumer().beginMonitorCostumer("/"+ip, process);
                    getMyConection().sendSQLQuery("SELECT LETTERCOMPUTERROOM FROM COMPUTER WHERE NUMBER="+jComboBox2.getSelectedItem().toString()+";");
                    String computerRoom=getMyConection().getLastResultSetColumnStrings(1)[0];
                    getLoanTable().addLoan(Integer.parseInt(jTextField1.getText()), jComboBox1.getSelectedItem().toString(), computerRoom, ip, Integer.parseInt(jComboBox2.getSelectedItem().toString()));
                    getMyConection().sendSQL("UPDATE COMPUTER SET STATE=5 WHERE NUMBER="+jComboBox2.getSelectedItem().toString()+";");
                    this.setVisible(false);
                    getPaternFrame().getContentPane().remove(this);
                    JOptionPane.showMessageDialog(null, "Prestamo exitoso", "Prestamo Exitoso", 1);

                }else{
                    JOptionPane.showMessageDialog(null, "Usuario en baja, No le es posible dar prestamo", "Usuario baja", 1);
                }
            }else{
                JOptionPane.showMessageDialog(null, "El usuario no esta registrado ", "Usuario no encontrado", 1);
            }
        }else{
            JOptionPane.showMessageDialog(null, "El usuario ya tine un prestamo activo actualmente ", "Prestamo", 1);
        }

        
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
